package alpaca.network;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;

/**
 * This class is only used to perform JUnit tests.
 *
 */

public class MockClientConnection implements IClientConnection {

	
	private PropertyChangeSupport pcs1;
	private PropertyChangeSupport pcs2;

	
	public MockClientConnection() {
		pcs1 = new PropertyChangeSupport(this);
		pcs2 = new PropertyChangeSupport(this);
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl) {
		pcs1.addPropertyChangeListener(pcl);

	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl) {
		pcs1.removePropertyChangeListener(pcl);
	}
	
	public void addMockPropertyChangeListener(PropertyChangeListener pc2) {
		pcs2.addPropertyChangeListener(pc2);

	}

	public void removeMockPropertyChangeListener(PropertyChangeListener pc2) {
		pcs2.removePropertyChangeListener(pc2);
	}


	@Override
	public void send(int connID, NetworkMessage o) {

		switch (o.getType()) {
		case LOGIN_SUCCESS:
			pcs2.firePropertyChange("Mock Login Success", null, new NetworkMessage(o.getObject(), o.getType()));
			break;
		case LOGIN_FAIL:
			pcs2.firePropertyChange("Mock Login Fail", null, new NetworkMessage(o.getObject(), o.getType()));
			break;
		case MESSAGE:
			pcs2.firePropertyChange("Mock Message", null, new NetworkMessage(o.getObject(), o.getType()));
			break;
		case STATUS_UPDATE:
			pcs2.firePropertyChange("Mock Status Update", null, new NetworkMessage(o.getObject(), o.getType()));
			break;
		case NEW_CONTACT_LIST:
			pcs2.firePropertyChange("Mock ContactList Update", null, new NetworkMessage(o.getObject(), o.getType()));
			break;
		case SEARCH_RESULTS:
			pcs2.firePropertyChange("Mock Search Results", null, new NetworkMessage(o.getObject(), o.getType()));
			break;
		}
		
	}


	@Override
	public void start(int port) throws IOException {
		//Do nothing		
	}


	@Override
	public void stop() {
		//Do nothing
	}


	@Override
	public void closeConnection(int connID) {
		//Do nothing
	}
	
	public void firePropertyChange(String string, NetworkMessage nm){
		nm.setFromConnectionId(1);
		pcs1.firePropertyChange(string, null, nm);
	}
}
